package cn.hp.cms.utils;

// 少了jar包
import com.mchange.v2.c3p0.ComboPooledDataSource;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

public class DBUtil {

    private static ComboPooledDataSource dataSource = null;

    private static Connection connection = null;
    private static PreparedStatement preparedStatement = null;
    private static ResultSet resultset = null;

    /**
     * 初始化配置
     */
    static{
        InputStream inputStream = null;
        try {
            //获取文件输入流
            inputStream = DBUtil.class.getClassLoader().getResourceAsStream("db.properties");

            //获取properties对象
            Properties properties = new Properties();

            //加载配置文件
            properties.load(inputStream);
            //加载驱动
            Class.forName(properties.getProperty("driver"));

            //实例化连接池
            dataSource = new ComboPooledDataSource();

            //配置连接池
            dataSource.setJdbcUrl(properties.getProperty("url"));
            dataSource.setUser(properties.getProperty("username"));
            dataSource.setPassword(properties.getProperty("password"));
            dataSource.setMinPoolSize(10);
            dataSource.setMaxPoolSize(100);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }
    }

    /**
     * @param sql sql语句
     * @param obj Object数组存储占位符内容
     * @return 影响的数据条数
     * 添加、修改、删除一条或多条数据
     */
    public static int update(String sql, Object [] obj){
        int update = 0;
        try {
            //获取连接
            connection = dataSource.getConnection();
            preparedStatement = connection.prepareStatement(sql);
            if(obj.length > 0){
                for(int i = 0; i<obj.length; i++) {
                    preparedStatement.setObject(i+1,obj[i]);
                }
            }
            update = preparedStatement.executeUpdate();
            return update;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            close();
        }
        return update;
    }

    /**
     * @param sql sql语句
     * @param obj 条件数组
     * @return 查询到的数据结果集
     * 根据条件查询一条数据
     */
    public static ResultSet select(String sql,Object [] obj){
        try {
            connection = dataSource.getConnection();
            preparedStatement = connection.prepareStatement(sql);
            if(obj.length > 0){
                for(int i = 0; i<obj.length; i++) {
                    preparedStatement.setObject(i+1,obj[i]);
                }
            }
            resultset = preparedStatement.executeQuery();
            return resultset;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 释放资源
     */
    public static void close(){
        try{
            if(resultset != null){
                resultset.close();
            }
            if(preparedStatement != null){
                preparedStatement.close();
            }
            if(connection != null){
                connection.close();
            }
        }catch (SQLException e){
            e.printStackTrace();
        }
    }
}

